package com.orange.core.service.carpark;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.carpark.CarParkOrderMapper;
import com.orange.core.mapper.user.UserMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.carpark.CarParkOrder;
import com.orange.core.pojo.entity.user.User;
import com.orange.core.pojo.req.carpark.CarParkOrderPageReq;
import com.orange.core.pojo.rsp.CarParkOrderExcel;
import com.orange.core.util.Constants;
import com.orange.core.util.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class CarParkOrderService extends ServiceImpl<CarParkOrderMapper, CarParkOrder> {

    private final UserMapper userMapper;

    public Page<CarParkOrder> page(PageReqDto<CarParkOrderPageReq> dto, Integer userId) {
        CarParkOrderPageReq condition = dto.getCondition();
        Page<CarParkOrder> page = this.lambdaQuery()
                .eq(userId != null, CarParkOrder::getUserId, userId)
                .eq(StrUtil.isNotBlank(condition.getPlateNo()), CarParkOrder::getPlateNo, condition.getPlateNo())
                .orderByDesc(CarParkOrder::getId)
                .page(dto.toPage());
        List<CarParkOrder> carParkOrders = page.getRecords();
        if (CollUtil.isNotEmpty(carParkOrders)) {
            List<Integer> userIds = carParkOrders.stream().map(CarParkOrder::getUserId).toList();
            Map<Integer, String> userMap = userMapper.selectBatchIds(userIds)
                    .stream().collect(Collectors.toMap(User::getId, User::getNickname));
            for (CarParkOrder carParkOrder : carParkOrders) {
                carParkOrder.setNickname(userMap.get(carParkOrder.getUserId()));
            }
        }
        return page;
    }

    public CarParkOrder info(int id) {
        CarParkOrder carParkOrder = this.getById(id);
        if (carParkOrder == null) {
            return null;
        }
        User user = userMapper.selectById(carParkOrder.getUserId());
        carParkOrder.setNickname(user.getNickname());
        return carParkOrder;
    }

    @SneakyThrows
    public void export(HttpServletResponse response) {
        List<CarParkOrder> carParkOrders = this.list();
        if (CollUtil.isNotEmpty(carParkOrders)) {
            List<Integer> userIds = carParkOrders.stream().map(CarParkOrder::getUserId).toList();
            Map<Integer, String> userMap = userMapper.selectBatchIds(userIds)
                    .stream().collect(Collectors.toMap(User::getId, User::getNickname));

            List<CarParkOrderExcel> results = new ArrayList<>();
            for (CarParkOrder carParkOrder : carParkOrders) {
                results.add(CarParkOrderExcel.builder()
                        .nickname(userMap.get(carParkOrder.getUserId()))
                        .plateNo(carParkOrder.getPlateNo())
                        .parkName(carParkOrder.getParkName())
                        .entryTime(carParkOrder.getEntryTime().format(Constants.DEFAULT_DATE_TIME_FORMATTER))
                        .parkDuration(carParkOrder.getParkDuration())
                        .totalAmount(carParkOrder.getTotalAmount())
                        .paidCredit(carParkOrder.getPaidCredit())
                        .deductAmount(carParkOrder.getDeductAmount())
                        .couponSolutionNo(carParkOrder.getCouponSolutionNo())
                        .createTime(carParkOrder.getCreateTime().format(Constants.DEFAULT_DATE_TIME_FORMATTER))
                        .build());
            }
            ExcelUtil.write(results, CarParkOrderExcel.class, response, "停车订单导出");
        }
    }
}
